package com.foreveredu.propms.persist.hibernate;

import java.util.List;

import com.foreveredu.propms.domain.StrongNews;
import com.foreveredu.propms.domain.PageModel;
import com.foreveredu.propms.persist.IStrongNewsDao;

public class HibernateStrongNewsDao extends HibernateAbstractDao<StrongNews, Integer>
		implements IStrongNewsDao {

	private static final long serialVersionUID = 1L;

	public HibernateStrongNewsDao() {
		super(StrongNews.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<StrongNews> findStrongNewsListView(int belong) {
		List<StrongNews> StrongNews = this.getHibernateTemplate().find(
				"from StrongNews n where n.belong=? order by n.id desc ", belong);
		// 如果新闻列表长度超过6条，则只提取前6条新闻
		if (StrongNews != null && StrongNews.size() > 6) {
			return StrongNews.subList(0, 6);
		}
		return StrongNews;
	}

	@SuppressWarnings("unchecked")
	@Override
	public PageModel findStrongNewsByPage(int offset, int belong) {
		List<StrongNews> StrongNews = this.getHibernateTemplate().find(
				"from StrongNews n where n.belong=? order by n.id desc", belong);
		PageModel pm = new PageModel();
		pm.setTotal(StrongNews.size());
		if (offset + 10 > StrongNews.size()) {
			pm.setDatas(StrongNews.subList(offset, StrongNews.size()));
			return pm;
		}
		pm.setDatas(StrongNews.subList(offset, offset + 10));
		return pm;
	}

	@Override
	@SuppressWarnings("unchecked")
	public StrongNews findStrongNewsByState() {
		List<StrongNews> StrongNewsList = this.getHibernateTemplate().find(
				"from StrongNews n where n.state=?", 1);
		StrongNews StrongNews = null;
		if (StrongNewsList != null && StrongNewsList.size() > 0) {
			StrongNews = StrongNewsList.get(0);
		}
		return StrongNews;
	}

	@Override
	@SuppressWarnings("unchecked")
	public StrongNews findStrongNewsById(int id) {
		List<StrongNews> StrongNewsList = this.getHibernateTemplate().find(
				"from StrongNews n where n.id=?", id);
		StrongNews StrongNews = null;
		if (StrongNewsList != null && StrongNewsList.size() > 0) {
			StrongNews = StrongNewsList.get(0);
		}
		return StrongNews;
	}

	@SuppressWarnings("unchecked")
	@Override
	public StrongNews findStrongNewsByBelong(int belong) {
		List<StrongNews> StrongNewsList = this.getHibernateTemplate().find(
				"from StrongNews n where n.belong= ?", belong);
		StrongNews StrongNews = null;
		if (StrongNewsList != null && StrongNewsList.size() > 0) {
			StrongNews = StrongNewsList.get(0);
		}
		return StrongNews;
	}
}
